/**
* @File:mp3player.h
* @Version:V1.0
* @Author:rudius
* @Brief:
* @Logs:
* 	Date		Author		Notes
* 	2025-07-09	rudius		first version
*/

#ifndef __MP3PLAYER_H
#define __MP3PLAYER_H
// <<< Use Configuration Wizard in Context Menu >>>

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stddef.h>
#include <rtthread.h>
#include "ff.h"

#include "minimp3.h"
#ifdef __cplusplus
extern "C" {
#endif

/*----------------------------------------------------------------------------*/
/* Public define -------------------------------------------------------------*/
/* Public macro --------------------------------------------------------------*/
/* Public typedef ------------------------------------------------------------*/
/* 播放状态枚举 */
typedef enum {
    MP3_STOPPED = 0,
    MP3_PLAYING,
    MP3_PAUSED,
    MP3_ERROR
} mp3_player_state_t;

/* 播放器对象结构体 */
struct mp3_player {
    /* 播放控制 */
    mp3_player_state_t state;
    rt_mutex_t state_mutex;
    rt_uint32_t sample_rate;
    rt_uint8_t volume;
    
    /* 文件系统 */
    FATFS fatfs;        // 添加FATFS实例
    FIL file;
    char* file_path;
    
    /* 音频处理 */
    mp3dec_t decoder;
    mp3dec_frame_info_t frame_info;
    uint8_t* input_buf;
    rt_size_t buf_size;
    
    /* 双缓冲机制 */
    int16_t* audio_buf[2];
    rt_uint8_t active_buf;
    
    /* 线程控制 */
    rt_thread_t decode_thread;
    rt_thread_t play_thread;
    rt_sem_t buf_ready;
    rt_mutex_t i2s_mutex;
};

/* Public variables ----------------------------------------------------------*/
/* Public function prototypes ------------------------------------------------*/
/**
 * 初始化MP3播放器对象
 * @param player 播放器对象指针
 * @return RT_EOK 成功，其他 错误码
 */
int mp3_player_init(struct mp3_player* player);

/**
 * 打开MP3文件
 * @param player 播放器对象指针
 * @param path 文件路径
 * @return RT_EOK 成功，其他 错误码
 */
int mp3_player_open(struct mp3_player* player, const char* path);

/**
 * 开始播放
 * @param player 播放器对象指针
 * @return RT_EOK 成功，其他 错误码
 */
int mp3_player_play(struct mp3_player* player);

/**
 * 暂停播放
 * @param player 播放器对象指针
 * @return RT_EOK 成功，其他 错误码
 */
int mp3_player_pause(struct mp3_player* player);

/**
 * 停止播放
 * @param player 播放器对象指针
 * @return RT_EOK 成功，其他 错误码
 */
int mp3_player_stop(struct mp3_player* player);

/**
 * 设置音量
 * @param player 播放器对象指针
 * @param vol 音量值 (0-100)
 * @return RT_EOK 成功
 */
int mp3_player_set_volume(struct mp3_player* player, rt_uint8_t vol);

/**
 * 释放播放器资源
 * @param player 播放器对象指针
 */
void mp3_player_deinit(struct mp3_player* player);

#ifdef __cplusplus
}
#endif

// <<< end of configuration section >>>
#endif
/*------------------------------end of mp3player.h---------------------------------*/